// pages/banner_detail/banner_detail.js
let util = require("../../utils/util.js");
let WxParse = require('../../wxParse/wxParse.js');
Page({
    //分享
    onShareAppMessage: function () { },
    /**
     * 页面的初始数据
     */
    data: {
        dianzan_logo:"../../images/index/zan.png",
        dianzan_logo2: "../../images/index/zan2.png",
        count_zan:206,
        bannerId:'',
        detail:{},
        myPublish:'',
        pageNum: 1,
        readMoreFlag: false,
        imgHeight:0
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (e) {
        var system = wx.getSystemInfoSync();
        this.setData({
            imgHeight: 300 * system.screenWidth / 750,
            bannerId: e.id
        });
        this.bannerDetail();
    },
    //轮播图详情
    bannerDetail: function(){
        let userInfo = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.id;
        let that = this;
        util.wxAjax("glBanner/detail","post",{
            id: this.data.bannerId,
            adminid: userInfo
        },function(res){
            var data = res.data.data;
            WxParse.wxParse('article1', 'html', data.glBanner.content, that, 0);
            that.setData({
                detail: data,
                readMoreFlag: data.commentList.length>19?true:false
            })
        })
    },
    //加载更多评论
    getMoreComment:function(){
        var that = this;
        var id = this.data.bannerId;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        var pageNum = this.data.pageNum;
        var detail = this.data.detail;
        util.wxAjax("glBanner/bannermoreCommon","post",{
            id:id,
            adminid:userInfo.smAdmin.id,
            pageNum: pageNum
        },function(res){
            var data = res.data;
            if(data.result == 200 || data.result == "200"){
                detail.commentList = detail.commentList.concat(data.data);
                that.setData({
                    detail: detail,
                    readMoreFlag: data.data.length>19?true:false
                });
            }else{
                wx.showToast({
                    title: data.msg,
                    icon:"none"
                })
            }
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    //发表评论
    publish: function () {
        if (this.data.myPublish) {
            var userInfo = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.id;
            var that = this;
            util.wxAjax("glBanner/updateCommentsAddOne", "post", {
                id: that.data.bannerId,
                adminid: userInfo,
                content: that.data.myPublish
            }, function (res) {
                that.setData({
                    myPublish: ''
                });
                if (res.data.result != 200) {
                    wx.showToast({
                        title: '评论失败',
                        icon: 'none'
                    })
                }else{
                    that.bannerDetail();
                }
            })
        }else{
            wx.showToast({
                title: '评论内容不能为空',
                icon: 'none'
            })
        }
    },
    //获取留言板的内容
    getTextArea: function (e) {
        var value = e.detail.value;
        this.setData({
            myPublish: value
        })
    },
    //对评论点赞
    dianzan:function(e){
        var id = e.currentTarget.dataset.id;
        var contentid = e.currentTarget.dataset.contentid;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.id;
        var index = e.currentTarget.dataset.index;
        var commentList = this.data.detail.commentList;
        let that = this;
        util.wxAjax("glBanner/updateLaudsAddOne","post",{
            contentId: contentid,
            adminid: userInfo,
            id: id
        },function(res){
            var data = res.data;
            if(data.result == 200 || data.result == "200"){
                commentList[index].lauds = commentList[index].lauds*1+1;
                commentList[index].isLauds = 1;
                var detail = that.data.detail;
                detail.commentList = commentList;
                that.setData({
                    detail: detail
                });
            }
        })
    },
    //上拉刷新
    onPullDownRefresh: function () {
        // 显示顶部刷新图标  
        wx.showNavigationBarLoading();
        this.setData({
            pageNumber: 1,
            detail: {}
        });
        this.bannerDetail();
    },
    //下拉加载
    onReachBottom: function () {
        var flag = this.data.readMoreFlag;
        if (flag) {
            var pageNum = this.data.pageNum;
            // 显示加载图标  
            wx.showLoading({
                title: '玩命加载中',
            })
            this.setData({
                pageNum: ++pageNum,
            });
            this.getMoreComment();
        }
    },
    //删除评论
    deleteContent:function(options){
        var that = this;
        var id = options.currentTarget.dataset.id;
        var index = options.currentTarget.dataset.index;
        var detail = this.data.detail;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        wx.showModal({
            title: '确认信息',
            content: '确认要删除当前评论吗？',
            success: function (res) {
                util.wxAjax("glBanner/deleteCommon", "post", {
                    id: id,
                    adminid: userInfo.smAdmin.id
                }, function (res) {
                    var data = res.data;
                    if (data.result == 200 || data.result == "200") {
                        detail.commentList.splice(index, 1);
                        that.setData({
                            detail: detail
                        });
                        wx.showToast({
                            title: '删除成功！',
                            icon: "none"
                        })
                    } else {
                        wx.showToast({
                            title: data.msg,
                            icon: "none"
                        })
                    }
                })
            }
        });
    }
})